import {
  getToken,
  checkWhite
} from '@/utils/auth'

// 登录页面
const loginPage = "/pages/login"

// 页面白名单
const whiteList = [
  '/pages/login', '/pages/common/webview/index', '/pages/register', '/pages/socialLogin'
]
// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
  uni.addInterceptor(item, {
    invoke(to) {
      if (getToken()) {
        if (to.path === loginPage) {
          uni.reLaunch({
            url: "/"
          })
        }
        return true
      } else {
        if (checkWhite(to.url, whiteList)) {
          return true
        }
        uni.reLaunch({
          url: loginPage
        })
        return false
      }
    },
    fail(err) {
      console.log(err)
    }
  })
})